//
//  XMLTabbar.swift
//  Weibo
//
//  Created by mac on 16/5/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

protocol XMLTabbarDelegate :NSObjectProtocol {
    
    func composeButtonClick()
}


class XMLTabbar: UITabBar {
    
    var composeButtonClosure :(() -> ())?
    
    weak var xmlDelegate :XMLTabbarDelegate?

    private lazy var composeButton :UIButton = {
    
        let button = UIButton()
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.sizeToFit()
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(composeButton)
        
    }
    //使用xib或者storyboard创建视图的时候会执行
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center.x = frame.width * 0.5
        composeButton.center.y = frame.height * 0.5
//        print(subviews)
        var index = 0
        let childViewWidth = frame.width / 5
        for childViwe in subviews {
            if childViwe.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
                childViwe.frame.size.width = childViewWidth
                childViwe.frame.origin.x = childViewWidth * CGFloat(index)
                index++
                
                if index == 2 {
                
                    index++
                }
                
            }
        
            
        }
    }
    
    
    @objc private func composeButtonClick() {
    
        composeButtonClosure?()
        xmlDelegate?.composeButtonClick()
    }

}
